{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/142_google_maps.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Using Google Maps as basemaps**\n",
    "\n",
    "Uncomment the following line to install [geemap](https://geemap.org) if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install -U geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "To use Google Maps as basemaps, you need to sign up for a Google Maps API key. Follow the instructions on the [Google Maps Platform](https://developers.google.com/maps/get-started) website to get an API key. The following steps are required:\n",
    "- Step 1: [Set up a Google Cloud project](https://developers.google.com/maps/get-started#create-project)\n",
    "- Step 2: [Enable Google Maps Platform APIs and SDKs ](https://developers.google.com/maps/get-started#enable-api-sdk)\n",
    "- Step 3: [Get an API key](https://developers.google.com/maps/get-started#api-key)\n",
    "\n",
    "\n",
    "There is a cost for requesting Maps tiles, but there is a \\$200 monthly credit and we've been told that for the purposes of typical EE-type usage, it would be rare to exceed \\$200.\n",
    "\n",
    "Once you have your API key, you can set it as a Google Colab secret with the name `GOOGLE_MAPS_API_KEY`. Alternatively, you can set it as an environment variable on your local machine. Uncomment the following line to set the environment variable if you are running this notebook locally."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import os\n",
    "# os.environ[\"GOOGLE_MAPS_API_KEY\"] = \"<YOUR-API-KEY>\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6",
   "metadata": {},
   "source": [
    "If the `GOOGLE_MAPS_API_KEY` environment variable is set, geemap will automatically detect it and use it to request Google Maps tiles. By default, geemap will use Google Roadmap as the basemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/jm80iKK.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "Use Google Hybrid basemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(basemap=\"Google.Hybrid\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/4vLGh6X.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "To use multiple basemaps, you can use the `Map.add_basemap()` function."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "m.add_basemap(\"Google.Hybrid\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "14",
   "metadata": {},
   "source": [
    "Use Google Satellite basemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(basemap=\"Google.Satellite\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/mJyE1jT.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "Use Google Terrain basemap."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map(basemap=\"Google.Terrain\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19",
   "metadata": {},
   "source": [
    "Google Maps can be customized with various parameters. To create a custom Google Maps basemap, you can use the `GoogleMapsTileProvider` class. See <https://bit.ly/3UhbZKU> for more information on the available parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "from geemap.basemaps import GoogleMapsTileProvider"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "style = [\n",
    "    {\"stylers\": [{\"hue\": \"#00ffe6\"}, {\"saturation\": -20}]},\n",
    "    {\n",
    "        \"featureType\": \"road\",\n",
    "        \"elementType\": \"geometry\",\n",
    "        \"stylers\": [{\"lightness\": 100}, {\"visibility\": \"simplified\"}],\n",
    "    },\n",
    "]\n",
    "\n",
    "m = geemap.Map(center=[40, -100], zoom=4)\n",
    "basemap = GoogleMapsTileProvider(\n",
    "    map_type=\"roadmap\",\n",
    "    region=\"CN\",\n",
    "    language=\"zh-Cn\",\n",
    "    scale=\"scaleFactor2x\",\n",
    "    highDpi=True,\n",
    "    style=style,\n",
    ")\n",
    "m.add_basemap(basemap)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/GKWDi4U.png)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
